import Vue from 'vue';
import Vuex from 'vuex';
import {
	getMailCount
} from '@/api/business/tbMail';
import {
	queryUserRoleByType
} from '@/api/system/tbSysUserRole';
Vue.use(Vuex)
const state = {
	messageCount: 0,
	principalData: []
}
const getters = {}
const actions = {
	//获取用户数据 - 通过角色类型
	async getUserByRoleType({
		commit
	}) {
		let params = {
			page: 1,
			limit: 99999,
			type: '3'
		};
		let result = await queryUserRoleByType(params).then(res => res.data);
		if (result.code === '0') {
			let datas = (result.data || []).map(item => {
				return {
					id: item.id,
					userName: item.userName,
					filed10: item.filed10
				}
			});
			commit('SET_PRINCIPAL_DATA', datas);
			return datas;
		} else {
			Promise.reject(new Error('faile'));
		}
	},

	//获取未读信息数量
	async getUnreadMailCount({
		commit
	}, auth) {
		let result = await getMailCount().then(res => res.data);
		if (result.code === '0') {
			commit('SET_UNREADMAIL_COUNT', Number(result.msg));
		} else {
			Promise.reject(new Error('faile'));
		}
	}
}
const mutations = {
	SET_PRINCIPAL_DATA: (state, d) => {
		state.principalData = d;
		Vue.prototype.$local.set('principalData', d);
		// Vue.prototype.$local.set('principalData', Vue.prototype.$cryptoEncrypt(d));
	},
	SET_UNREADMAIL_COUNT: (state, d) => {
		state.messageCount = d;
	}
}
export default {
	state,
	getters,
	actions,
	mutations
}